﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BookStore.Solution.BLL;
using BookStore.Solution.Entities;
using BookStore.Solution.Utilities;
using DevComponents.DotNetBar;
namespace BookStore.Solution.Presentation
{
    public partial class ucEmployee : UserControl
    {
        private EmployeeManager manager = new EmployeeManager();
        private DataTable MyEmployeeTable = null;

        private void LoadData(DataTable table)
        {
            dgvList.AutoGenerateColumns = false;
            dgvList.DataSource = table;
            for (int i = 0; i < dgvList.Columns.Count; i++)
            {
                dgvList.Columns[i].DataPropertyName = dgvList.Columns[i].Name;
            }
        }

        private object[] CreateItemArray(Employee employee)
        {
            return new object[] { employee.EmployeeID, employee.EmployeeName, employee.Birthday, employee.Gender, employee.IndentityCard, employee.Address, employee.Email, employee.PhoneNumber, employee.Position, employee.Status };
        }

        private Employee GetSelectedRows()
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                Employee entity = new Employee();
                entity.EmployeeID = DataUtil.ToString(dgvList.SelectedRows[0].Cells[0].Value);
                entity.EmployeeName = DataUtil.ToString(dgvList.SelectedRows[0].Cells[1].Value);
                entity.Birthday = DataUtil.ToDate(dgvList.SelectedRows[0].Cells[2].Value);
                entity.Gender = DataUtil.ToBoolean(dgvList.SelectedRows[0].Cells[3].Value);
                entity.IndentityCard = DataUtil.ToString(dgvList.SelectedRows[0].Cells[4].Value);
                entity.Address = DataUtil.ToString(dgvList.SelectedRows[0].Cells[5].Value);
                entity.Email = DataUtil.ToString(dgvList.SelectedRows[0].Cells[6].Value);
                entity.PhoneNumber = DataUtil.ToString(dgvList.SelectedRows[0].Cells[7].Value);
                entity.PositionID = DataUtil.ToInt(dgvList.SelectedRows[0].Cells[8].Value);
                entity.Status = DataUtil.ToString(dgvList.SelectedRows[0].Cells[9].Value);
                return entity;
            }
            else
                return null;
        }

        public ucEmployee()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmEmployee frm = new frmEmployee();
            frm.ShowDialog();
            if (frm.NewEmployee != null)
            {
                MyEmployeeTable.LoadDataRow(CreateItemArray(frm.NewEmployee), false);
                LoadData(MyEmployeeTable);
            }
        }
        
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (MessageBox.Show("Bạn có chắc chắn xóa không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    Employee employee = GetSelectedRows();
                    string result = manager.Delete(employee);
                    if (result == "Xóa thành công")
                    {
                        MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        MyEmployeeTable.Rows.RemoveAt(dgvList.SelectedRows[0].Index);
                        LoadData(MyEmployeeTable);
                    }
                    else
                    {
                        MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            btnSearch.Expanded = true;
        }

        private void biSearchBy_Click(object sender, EventArgs e)
        {
            frmSearchEmployee frm = null;
            string text = "";
            if (sender is ButtonItem)
                text = (sender as ButtonItem).Text;
            else
                text = (sender as ToolStripMenuItem).Text;
            switch (text)
            {
                case "Tìm theo mã":
                    frm = new frmSearchEmployee(SearchEmployeeType.ID);
                    break;
                case "Tìm theo tên":
                    frm = new frmSearchEmployee(SearchEmployeeType.Name);
                    break;
                case "Tìm theo chức vụ":
                    frm = new frmSearchEmployee(SearchEmployeeType.Position);
                    break;
            }
            frm.ShowDialog();
            if (frm.ResultTable != null)
            {
                LoadData(frm.ResultTable);
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            MyEmployeeTable = manager.GetAll();
            LoadData(MyEmployeeTable);
        }

        private void dgvList_DoubleClick(object sender, EventArgs e)
        {
            btnUpdate.PerformClick();
        }

        private bool SetRole()
        {
            bool t = false;
            List<string> roles = AccountManager.GetRoleName();
            btnAdd.Enabled = btnDelete.Enabled = btnUpdate.Enabled = false;
            btnSearch.Enabled = false;
            if (roles.Contains("Thêm, xóa, cập nhật nhân viên"))
            {
                btnAdd.Enabled = btnDelete.Enabled = btnUpdate.Enabled = true;
                t = true;
            }
            if (roles.Contains("Tìm kiếm nhân viên"))
            {
                btnSearch.Enabled = true;
                t = true;
            }
            return t;
        }

        private void ucEmployee_Load(object sender, EventArgs e)
        {
            if (SetRole())
            {
                MyEmployeeTable = manager.GetAll();
                LoadData(MyEmployeeTable);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                Employee entity = GetSelectedRows();
                frmEmployee fr = new frmEmployee(entity);
                fr.ShowDialog();
                if (fr.NewEmployee != null)
                {
                    MyEmployeeTable.Rows[dgvList.SelectedRows[0].Index].ItemArray = CreateItemArray(fr.NewEmployee);
                    LoadData(MyEmployeeTable);
                }
            }
        }
    }
}
